package com.xscha.tableforge.controller;

import com.xscha.soyue.common.core.domain.R;
import com.xscha.tableforge.model.entity.Space;
import com.xscha.tableforge.model.vo.PageReq;
import com.xscha.tableforge.model.vo.PageResult;
import com.xscha.tableforge.model.vo.SpaceVo;
import com.xscha.tableforge.service.SpaceService;
import com.xscha.tableforge.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/spaces")
public class SpaceController {

    private final SpaceService spaceService;

    @Autowired
    public SpaceController(SpaceService spaceService) {
        this.spaceService = spaceService;
    }

    @GetMapping
    public R<List<SpaceVo>> getAllSpaces() {
        List<Space> spaces = spaceService.findAll();
        return R.ok(spaces.stream().map(this::convertToVo).collect(Collectors.toList()));
    }
    
    @PostMapping("/page")
    public R<PageResult<SpaceVo>> getSpacesPage(@RequestBody PageReq pageRequest) {
        PageResult<Space> result = spaceService.findPage(pageRequest);
        List<SpaceVo> spaceVos = result.getRecords().stream()
                .map(this::convertToVo)
                .collect(Collectors.toList());
        return R.ok(PageResult.of(spaceVos, result.getTotal(), result.getPageNum(), result.getPageSize()));
    }

    @GetMapping("/{id}")
    public R<SpaceVo> getSpaceById(@PathVariable String id) {
        return spaceService.findById(id)
                .map(space -> R.ok(convertToVo(space)))
                .orElse(R.fail("空间不存在"));
    }

    @PostMapping
    public R<SpaceVo> createSpace(@RequestBody Space space) {
        Space createdSpace = spaceService.createSpace(space, SecurityUtils.getUserId());
        return R.ok(convertToVo(createdSpace));
    }

    @PutMapping("/{id}")
    public R<SpaceVo> updateSpace(@PathVariable String id, 
                                 @RequestBody Space space) {
        if (!id.equals(space.getId())) {
            return R.fail("ID不匹配");
        }
        
        Space updatedSpace = spaceService.updateSpace(space, SecurityUtils.getUserId());
        return R.ok(convertToVo(updatedSpace));
    }

    @DeleteMapping("/{id}")
    public R<Void> deleteSpace(@PathVariable String id) {
        spaceService.deleteSpace(id, SecurityUtils.getUserId());
        return R.ok();
    }

    private SpaceVo convertToVo(Space space) {
        if (space == null) {
            return null;
        }
        SpaceVo vo = new SpaceVo();
        vo.setId(space.getId());
        vo.setName(space.getName());
        vo.setDescription(space.getDescription());
        vo.setLogo(space.getLogo());
        vo.setSpaceProps(space.getSpaceProps());
        vo.setCredit(space.getCredit());
        vo.setCreatedTime(space.getCreatedTime());
        vo.setCreatedBy(space.getCreatedBy());
        vo.setLastModifiedTime(space.getLastModifiedTime());
        vo.setLastModifiedBy(space.getLastModifiedBy());
        return vo;
    }
}